#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System.Drawing;
using System.Linq;
using AbstractClass;
using AbstractClass.Collections.Generic;
using NUnit.Framework;

namespace QuasarTest.Core
{
    [TestFixture]
    public class BinarySearchTreeTest
    {
        #region Setup/Teardown

        [SetUp]
        public void Init()
        {
            _binarySearchTree = new BinarySearchTree<int>();
            for (int i = 100; i >= 0; i--)
            {
                _binarySearchTree.Add(i);
            }
        }

        #endregion

        private BinarySearchTree<int> _binarySearchTree;

        [Test]
        public void Clear()
        {
            _binarySearchTree.Clear();
            Assert.AreEqual(_binarySearchTree.Count, 0);
        }

        [Test]
        public void TestAdd()
        {
            Assert.AreEqual(_binarySearchTree.Count, 101);
            Assert.IsTrue(_binarySearchTree.Contains(99));
            _binarySearchTree.AddRange(Enumerable.Range(101, 5));
            Assert.IsTrue(_binarySearchTree.Contains(103));
            Assert.AreEqual(_binarySearchTree.Count, 106);
        }

        [Test]
        public void TestCopyTo()
        {
            var intArrOne = new int[101];
            _binarySearchTree.CopyTo(intArrOne, 0, BinaryTreeTraversalOrder.InOrder);
            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(intArrOne[i] < intArrOne[i + 1]);
            }
        }

        [Test]
        public void TestExecuteAlgorithm()
        {
            var algo = new BinarySearchTreeSumAlgorithm<int>();
            int sum = _binarySearchTree.ExecuteAlgorithm(algo);
            Assert.AreEqual(sum, 5050);
        }

        [Test]
        public void TestFind()
        {
            BinaryTreeNode<int> node = _binarySearchTree.Find(35);
            Assert.NotNull(node);
            Assert.AreEqual(node.Value, 35);
            Assert.Greater(node.Value, node.Left.Value);
            Assert.Less(node.Value, node.Parent.Value);
        }

        [Test]
        public void TestMinMaxTest()
        {
            Assert.AreEqual(_binarySearchTree.Min, 0);
            Assert.AreEqual(_binarySearchTree.Max, 100);
        }

        [Test]
        public void TestRemove()
        {
            Assert.AreEqual(_binarySearchTree.Count, 101);
            _binarySearchTree.Remove(39);
            Assert.IsFalse(_binarySearchTree.Contains(39));
            Assert.AreEqual(_binarySearchTree.Count, 100);
            int no = _binarySearchTree.RemoveWhere(x => x > 90);
            Assert.AreEqual(_binarySearchTree.Count, 100 - no);
            Assert.AreEqual(_binarySearchTree.Max, 90);
            var drawer = new BinaryTreeDrawer<int>();
            Bitmap image = drawer.DrawImage(_binarySearchTree.Root);
            image.Save(@"C:\tree3.png");
        }
    }

    public class BinarySearchTreeSumAlgorithm<T> : IAlgorithm<BinarySearchTree<T>, T>
    {
        #region IAlgorithm<BinarySearchTree<T>,T> Members

        public T Run(BinarySearchTree<T> substrate)
        {
            var calculator = new GenericCalculator<T, T, T>();
            T sum = default(T);
            return substrate.Aggregate(sum, (current, element) => calculator.Add(element, current));
        }

        #endregion
    }
}